package com.ck.ck_back.service.impl;

import com.ck.ck_back.dao.CollectMapper;
import com.ck.ck_back.dao.ProductionMapper;
import com.ck.ck_back.dao.UserMapper;
import com.ck.ck_back.pojo.Collect;
import com.ck.ck_back.pojo.Production;
import com.ck.ck_back.pojo.User;
import com.ck.ck_back.service.collectservice;
import com.ck.ck_back.service.productionservice;
import com.ck.ck_back.service.userservice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 * @author: genggeng
 * @createDate: 2023/1/11 15:18
 * @descipton: 
 */
@Service
public class collectserviceImpl implements collectservice {
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProductionMapper productionMapper;
    @Resource
    private CollectMapper collectMapper;


//    添加收藏，404参数错误，4无此人，3无此作品，2加入失败，1正常
    @Transactional
    @Override
    /**
     * @author: genggeng
     * @createDate: 2023/1/11 15:18
     * @param user_id
     * @param production_id
     * @return: int 
     * @descipton: 
    */         
    public int addcollect(String user_id, int production_id) {
        if (!user_id.equals("")&& user_id != null &&!String.valueOf(production_id).equals("")){
            User user = userMapper.selectByPrimaryKey(user_id);
            if ( user!= null){
                Production production = productionMapper.selectByPrimaryKey(production_id);
                if (production!=null){
                    Collect collect = new Collect();
                    collect.setUserId(user_id);
                    collect.setProductionId(production_id);
                    collect.setCollectTime(new Date().getTime());
                    int result = collectMapper.insertSelective(collect);
                    if (result == 1){
                        return result;
                    }else {return 2;}
                }else {return 3;}
            }else {return 4;}
        }else {return 404;}
    }

//    1正常，2失败，3无此收藏，404参数错误
    @Transactional
    @Override
    /**
     * @author: genggeng
     * @createDate: 2023/1/11 15:15
     * @param user_id
     * @param production_id
     * @return: int
    */
    public int updatecollect(String user_id,int production_id) {
        if (!String.valueOf(production_id).equals("") && user_id !=null && !user_id.equals("")){
            Collect collect = collectMapper.selectbytwoid(user_id,production_id);
            if (collect!=null){
                if (collect.getState() == 0){
                    collect.setState(1);
                    collect.setCollectTime(new Date().getTime());
                }else {
                    collect.setState(0);
                }
                int result= collectMapper.updateByPrimaryKeySelective(collect);
                if (result == 1){
                    return result;
                }else {return 2;}
            }else {return 3;}
        }else {return 404;}
    }

    @Override
            
    public int getnum(String user_id, int production_id) {
        return collectMapper.select_collect_num(user_id,production_id);
    }

    @Override
    public int getstate(String user_id, int production_id) {
        if (user_id!=null &&!user_id.equals("")&&!String.valueOf(production_id).equals("")){
            Collect collect = collectMapper.selectbytwoid(user_id,production_id);
            if (collect!=null){
                return collect.getState();
            }else {return 0;}
        }else {return 404;}
    }

    @Override
    public List<Collect> getall(String user_id) {
        List<Collect> collects = collectMapper.getall(user_id);
            return collects;
    }

    @Override
    public int deletecollect(int collectId) {
        if(!String.valueOf(collectId).equals("")){
            Collect collect = collectMapper.selectByPrimaryKey(collectId);
            if (collect!=null){
                collect.setState(0);
                int result = collectMapper.updateByPrimaryKeySelective(collect);
                if (result == 1){
                    return result;
                }else {return 2;}
            }else {return 3;}
        }else {return 404;}
    }

//    初始话状态


}
